//
//  BTEProductThreeView.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTEProductThreeView: UIView {

    var clickArea:(()->())?
    
    var clickEnum:(()->())?
    
    let list = BTEBaseTableView.init(frame: .zero, style: .plain)
    
    let button = UIButton.init()
    
    var model: [BTECarnepalaeoglacioewelogyNetModel]? {
        didSet {
            guard let m = model else {
                return
            }
            list.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        button.setTitle(title: BTELanguageManager.shared.localizedString(forKey: "Next"))
        button.setBackgroundColor(UIColor.colorWithHex("#39B4E6")!)
        button.setFont(UIFont.systemFont(ofSize: 18, weight: .bold))
        button.setTitleColor(.white)
        button.roundCorners(.allCorners, radius: 25)
        button.layer.borderWidth = 2
        button.layer.borderColor = UIColor.colorWithHex("#6C2724")?.cgColor
        addSubview(button)
        button.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(38)
            make.bottom.equalToSuperview().offset(-34)
            make.height.equalTo(50)
        }
    
        list.backgroundColor = .clear
        addSubview(list)
        list.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(button.snp.top)
        }
        list.delegate = self
        list.dataSource = self
        list.separatorStyle = .none
        
        list.register(BTEListAreaTableViewCell.self, forCellReuseIdentifier: BTEListAreaTableViewCell.indetification)
        list.register(BTEListChoiseTableViewCell.self, forCellReuseIdentifier: BTEListChoiseTableViewCell.indetification)
        list.register(BTEListInputTableViewCell.self, forCellReuseIdentifier: BTEListInputTableViewCell.indetification)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
  

}


extension BTEProductThreeView: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (model ?? []).count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let m = model?[indexPath.row] {
            switch m.nondifferentiable! {
            case  "dodecatoic":
                guard let ce = tableView.dequeueReusableCell(withIdentifier: BTEListAreaTableViewCell.indetification, for: indexPath) as? BTEListAreaTableViewCell else {
                    return UITableViewCell()
                }
                ce.b1.text = m.aftermath
                ce.t1text.placeholder = m.unobstruct
                if let f = m.tripylaean , f.count > 0 {
                    ce.t1text.text = f
                }
                ce.model = m
                
                ce.callBack = {[weak self] str in
                    m.tripylaean = str
                    self?.list.reloadRows(at: [indexPath], with: .none)
                }
                
                return ce
            case "rewardably":
                guard let ce = tableView.dequeueReusableCell(withIdentifier: BTEListChoiseTableViewCell.indetification, for: indexPath) as? BTEListChoiseTableViewCell else {
                    return UITableViewCell()
                }
                
                ce.b1.text = m.aftermath
                ce.t1text.placeholder = m.unobstruct
                ce.model = m.panderism
                
                if let f = m.panderism?.filter({$0.choised == true}).first {
                    m.dikegrave = f.dikegrave
                    m.tripylaean = f.manificum
                    ce.t1text.text = f.manificum
                }
                
                ce.callBack = {[weak self] in
                    self?.list.reloadRows(at: [indexPath], with: .none)
                }
                
                return ce
            case "bombload":
                guard let ce = tableView.dequeueReusableCell(withIdentifier: BTEListInputTableViewCell.indetification, for: indexPath) as? BTEListInputTableViewCell else {
                    return UITableViewCell()
                }
                ce.model = m
                ce.b1.text = m.aftermath
                ce.t1text.placeholder = m.unobstruct
                let f = m.horsecraft ?? ""
                
                if f == "1" {
                    ce.t1text.keyboardType = .numberPad
                } else {
                    ce.t1text.keyboardType = .default
                }
                
                if let f = m.tripylaean , f.count > 0 {
                    ce.t1text.text = f
                }
                
                return ce
            default:
                return UITableViewCell()
            }
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let m = model?[indexPath.row] {
            switch m.nondifferentiable! {
            case  "dodecatoic":
                getTopViewController()?.view.endEditing(true)
                clickArea?()
            case "rewardably":
                getTopViewController()?.view.endEditing(true)
                clickEnum?()
            case "bombload":
               break
            default:
                break
            }
        }
    }
    
    private func getTopViewController() -> UIViewController? {
            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                  let rootViewController = windowScene.windows.first?.rootViewController else {
                return nil
            }
            
            var topViewController = rootViewController
            while let presentedViewController = topViewController.presentedViewController {
                topViewController = presentedViewController
            }
            
            return topViewController
        }
    
    
}


